﻿/******************************************************************************************
*                                                                                         *    
*                              Developed by Bruno Pires                                   *
*                                                                                         *
*                             email: bruno@blastersystems.com                             *
*                               web: www.blastersystems.com                               *
*                              blog: www.blastersystems.com/blog                          *
*                           twitter: @brunoacpires                                        *
*                                                                                         *
*             Software available under GNU LGPL v3 License without any guarantee          *
*                                                                                         *    
* *****************************************************************************************/ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Pap.Service;

namespace Pap.FrontEnd
{
    public partial class MainWindow : Window
    {

        
        public enum CameraStream
        {
            VGA,
            IR,
            Depth,
            Skl
        }

        private bool isRecording = false;
        private bool isPlaying = false;

        private PapKinect service;
        private CameraStream CameraType;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnDepth_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (service != null)
                {
                    service.StopServices();
                }
                CameraType = CameraStream.Depth;
                service = new PapKinect(PapKinect.KinectServiceType.Depth);
                service.KinectDataUpdated += new EventHandler(service_KinectDataUpdated);
                service.ProcessDepth();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            
        }

        private void btnIR_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (service != null)
                {
                    service.StopServices();
                }
                CameraType = CameraStream.IR;
                service = new PapKinect(PapKinect.KinectServiceType.IR);
                service.KinectDataUpdated += new EventHandler(service_KinectDataUpdated);
                service.ProcessInfraRed();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnVga_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (service != null)
                {
                    service.StopServices();
                }
                CameraType = CameraStream.VGA;
                service = new PapKinect(PapKinect.KinectServiceType.VGA);
                service.KinectDataUpdated += new EventHandler(service_KinectDataUpdated);
                service.ProcessVGA();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSkeleton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (service != null)
                {
                    service.StopServices();
                }
                CameraType = CameraStream.Skl;
                service = new PapKinect(PapKinect.KinectServiceType.Skl);
                service.KinectDataUpdated += new EventHandler(service_KinectDataUpdated);
                service.ProcessSkeleton();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRecorder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!isRecording)
                {
                    string filename = string.Empty;

                    MessageBox.Show("Click again to stop recording.");

                    Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                    dlg.DefaultExt = ".oni";
                    dlg.Filter = "OpenNI Recordings (.oni)|*.oni";
                    if (dlg.ShowDialog() == true)
                    {
                        filename = dlg.FileName;
                    }

                    isRecording = true;
                    if (service == null)
                    {
                        CameraType = CameraStream.Depth;
                        service = new PapKinect(PapKinect.KinectServiceType.Depth);
                        service.KinectDataUpdated += new EventHandler(service_KinectDataUpdated);
                    }
                    service.StartDepthRecording(filename);

                }
                else
                {
                    isRecording = false;
                    service.StopRecording();
                    MessageBox.Show("Stopped recording.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnPlayer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!isPlaying)
                {
                    MessageBox.Show("Click again to stop player.");

                    string filename = string.Empty;

                    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                    dlg.DefaultExt = ".oni";
                    dlg.Filter = "OpenNI Recordings (.oni)|*.oni";
                    if (dlg.ShowDialog() == true)
                    {
                        filename = dlg.FileName;
                    }
                    else
                        return;

                    isPlaying = true;
                    if (service == null)
                    {
                        service = new PapKinect(PapKinect.KinectServiceType.Depth);
                        service.PlayRecording(filename);
                        service.GetNewPlayerFrame += new EventHandler(service_GetNewPlayerFrame);
                    }
                }
                else
                {
                    isPlaying = false;
                    service.StopPlayer();
                    service.GetNewPlayerFrame -= service_GetNewPlayerFrame;
                    MessageBox.Show("Player stoppped.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void service_GetNewPlayerFrame(object sender, EventArgs e)
        {
            if (service!=null)
            {
                kinectData.Source = service.GetPlayerDepthFrame();   
            }
        }

        void service_KinectDataUpdated(object sender, EventArgs e)
        {
            switch (CameraType)
            {
                case CameraStream.VGA:
                    kinectData.Source = service.GetVGAFrame();
                    break;
                case CameraStream.IR:
                    kinectData.Source = service.GetInfraRedFrame();
                    break;
                case CameraStream.Depth:
                    kinectData.Source = service.GetDepthFrame();
                    break;
                case CameraStream.Skl:
                    kinectData.Source = service.GetSkeletonFrame();
                    break;
            }
        }
    }
}
